function check(param = {}) {
	// 合并默认参数
	console.info(param)
	// param = Object.assign({
	// 	title: "检测到有新版本！",
	// 	content: "请升级app到最新版本！",
	// 	canceltext: "暂不升级",
	// 	oktext: "立即升级",
	// 	barbackground:"rgba(50,50,50,0.8)",
	// 	barbackgroundactive:"rgba(32,165,58,1)"
	// }, param)
	// if (!param.api) {
	// 	return false;
	// }
	
	plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
		
		let platform = plus.os.name.toLocaleLowerCase()
		uni.request({
			url: 'http://121.229.107.81:81/api/version/getVersion',
			data: {
				platform: platform,
				version: widgetInfo.version
			},
			header: {
				'content-type': 'application/json'
			},
			method: 'POST',
			dataType: 'json',
			success: (result) => {
				console.info(result)
				let data = result.data ? result.data.data : null		
				if (data && data.code === 0 && data.url) {
					console.info(data,platform)
					if(/\.wgt$/i.test(data.url) || (platform == 'android' &&  /\.apk$/i.test(data.url))){
						// 如果是热更新  wgt 或 android平台下apk
						startdownload(param,data);
						return
					}
					if (platform == 'ios') {
						// 如果是ios,则跳转到appstore
						plus.runtime.openURL(result.data.data.url)
						
					}
				}else{//版本一致 只在我的  展示
					if(param.isShow != 1){//不是工作台
						uni.showToast({
							title: '已经是最新版本',
							duration: 2000
						});
					}
				}
			},
			fail: (res) => {
				console.info(res)
			}
		})
	});
}

function startdownload(param,data){
	uni.showModal({
		title: "检测到有新版本！",
		content: data.log ? data.log : "请升级app到最新版本！",
		showCancel: data.force ? false : true,
		confirmText: "立即升级",
		cancelText: "暂不升级",
		success: res => {
			if (!res.confirm) {
				console.log('取消了升级');
				return
			}
			if (data.shichang === 1 && /\.apk$/i.test(data.url)) {
				//去应用市场更新
				plus.runtime.openURL(data.shichangurl);
				plus.runtime.restart();
			} else {
				// 开始下载
				// 创建下载任务
				var dtask = plus.downloader.createDownload(data.url, {
						filename: "_downloads/"
					},
					function(d, status) {
						console.log('d',d)
						// 下载完成
						if (status == 200) {
							plus.runtime.install(d.filename, {
								force: false
							}, function() {
								//进行重新启动;
								plus.runtime.restart();
							}, (e) => {
								uni.showToast({
									title: '安装升级包失败:' + JSON.stringify(e),
									icon: 'none'
								})
							});
						} else {
							plus.nativeUI.alert("下载升级包失败，请手动去站点下载安装，错误码: " + status);
						}
				});
				
				let wrapwidth=parseInt(plus.screen.resolutionWidth / 2);
				let view = new plus.nativeObj.View("maskView", {
					backgroundColor: '#323232',
					left: (wrapwidth/2) + "px",
					bottom: "80px",
					width: wrapwidth+"px",
					height: "20px"
				});
				
				view.show()
				let viewinner = new plus.nativeObj.View("maskViewinner", {
					backgroundColor: '#ff0000',
					left: (wrapwidth/2)+"px",
					bottom: "80px",
					width: "1px",
					height: "20px"
				});
				viewinner.show()
				dtask.addEventListener("statechanged", (e) => {
					if (e && e.downloadedSize > 0) {
						let jindu = parseInt((e.downloadedSize / e.totalSize)*wrapwidth)
						viewinner.setStyle({width:jindu+'px'});
					}
				}, false);
				dtask.start();
			}
		}
	});
}

export default {
	check
}
